package mips;

import java.util.HashMap;
import java.util.Map;

public class R4300i {

	public static boolean DELAY_SLOT_EXEC = false;
	
	public static int PC;
	public static int nPC;
	public static long LO;
	public static long HI;
	/**
	 *	Registers<br/>
	 *	<br/>
	 *	Name 		Number 		Use 						Callee must preserve?<br/>
	 *	----		------		---							---------------------<br/>
	 *	$zero		$0 			constant 0 					N/A<br/>
	 *	$at			$1 			assembler temporary 		No<br/>
	 *	$v0�$v1		$2�$3 		Values for function returns	and expression evaluation No<br/>
	 *	$a0�$a3		$4�$7 		function arguments 			No<br/>
	 *	$t0�$t7		$8�$15 		temporaries 				No<br/>
	 *	$s0�$s7		$16�$23 	saved temporaries 			Yes<br/>
	 *	$t8�$t9		$24�$25		temporaries 				No<br/>
	 *	$k0�$k1		$26�$27 	reserved for OS kernel 		No<br/>
	 *	$gp			$28 		global pointer 				Yes<br/>
	 *	$sp			$29 		stack pointer 				Yes<br/>
	 *	$fp			$30 		frame pointer 				Yes<br/>
	 *	$ra			$31 		return address 				N/A
	 */
	public static long GPR[] = new long[32];
	public static long FPR[] = new long[32];

	public static Map<Integer,compiler.BasicBlock> targets = new HashMap<Integer,compiler.BasicBlock>();
	
	//public static Map<Integer,BasicBlock>
	
	public static final void init() {

		for(int i=0;i<1048576*2;i++) {
			n64.MemoryManager.RDRAM[i] = 0;
		}
		
		for(int i=0;i<32;i++) {
			
			GPR[i] = 0;
			FPR[i] = 0;
			mips.COP0.CONTROL[i] = 0;
		}
		
		// PIF ROM sets register values
		GPR[20] = 0x00000001;
	    GPR[22] = 0x0000003f;
	    GPR[29] = 0xA0401FF0;

	    // CONFIG
	    mips.COP0.CONTROL[16] = 0x0006e463;
	    // STATUS
	    mips.COP0.CONTROL[12] = 0x34000000;
	    // RANDOM
	    mips.COP0.CONTROL[ 2] = 0x0000002F;
	    // PR RV ID
	    mips.COP0.CONTROL[15] = 0x00000B00;
	    // RANDOM
	    mips.COP0.CONTROL[ 2] = 0x0000001F;
	    
	    //PIF ROM writes 0x01010101 to memory address 0x0430 0004
	    n64.MemoryManager.writeWord(0x01010101, 0x04300004);

	    /**
	     * The PIF ROM then copies the first 0x1000 bytes from the cartridge 
	     * (located at 0xb000 0000) to memory address 0xA400 0000.
	     * Next the PIF ROM sets the PC to 0xA400 0040. Note that this skips 
	     * the first 0x40 bytes of the program because that is where the 
	     * cartridge header is stored.
	     */
	    
	    // PIF ROM copies first 0x1000 bytes of ROM to first 0x1000 bytes of SP_MEM
	    for(int i=0;i<0x1000/4;i++) {
	    	
	    	n64.MemoryManager.SP_MEM[i] = n64.MemoryManager.ROM[i];
	    	//System.out.printf("%x \n", SP_MEM[i]);
	    }

	    //PC = 0xA0000000;
	    //nPC = 0xA0000004;
	    
	    //PC = 0xA4000040;
	    //nPC = 0xA4000044;
	    
	    PC  = 0xA0700000;
	    nPC = 0xA0700004;
	    
	    //PC = 0x80200000;
	    //nPC = 0x80200004;
	    
	    //PC = n64.MemoryManager.ROM[2];
	    //nPC = PC + 4;
	}
	

}
